<?php
/**
 * Yii view form
 *
 * @author luubk
 * @since Jan 11, 2013 - 2:46:02 PM
 */
?>

<?php
$this->beginWidget('MyActiveForm', array(
    'model' => $this->model,
    'title' => $this->pageTitle,
    'htmlOptions' => array('class' => 'span'),
    'columns' => array(
        'name',
        array(
            'field' => 'position',
            'type' => 'dropDownList',
            'data' => Menu::getArrayPosition(),
            'htmlOptions' => array('class' => ""),
        ),
        'rank',
        'icon',
    ),
));
?>
<div class="clear"></div>
<div class="grid-title">
    <div class="pull-left"><?php echo Yii::t('menu', 'Items') ?></div>
    <div class="pull-right"></div>
    <div class="clear"></div>
</div>
<div class="grid-content">
    <?php if ($this->model->items): ?>
        <?php foreach ($this->model->items as $item): ?>
            <div class="formRow">
                <input type="text" name="MenuItem[<?php echo $item->id ?>][name]" 
                       value="<?php echo $item->name ?>" placeholder="<?php echo Yii::t('menuitem', 'Name') ?>"/>
                <input type="text" name="MenuItem[<?php echo $item->id ?>][description]"
                       value="<?php echo $item->description ?>" placeholder="<?php echo Yii::t('menuitem', 'Description') ?>"/>
                <select class="" name="MenuItem[<?php echo $item->id ?>][type]">
                    <?php foreach (MenuItem::getArrayType() as $type => $type_name): ?>
                        <option value="<?php echo $type ?>" 
                                <?php echo $item->type == $type ? 'selected=""' : '' ?>>
                                    <?php echo $type_name ?>
                        </option>
                    <?php endforeach; ?>
                </select>
                <input type="text" name="MenuItem[<?php echo $item->id ?>][value]"
                       value="<?php echo $item->value ?>"  placeholder="<?php echo Yii::t('menuitem', 'Value') ?>"/>
                <input type="text" name="MenuItem[<?php echo $item->id ?>][rank]"
                       value="<?php echo $item->rank ?>"  placeholder="<?php echo Yii::t('menuitem', 'Rank') ?>"/>


                <a class="btn btn-mini btn-inverse" href="#" onclick="remove_item($(this), <?php echo $item->id ?>)">
                    <?php echo Yii::t('form', 'Delete') ?>
                </a>

                <div class="clear"></div>

                <!--Begin Menu Item--> 
                <?php
                if ($item->children == NULL)
                    echo NULL;
                else
                {
                    ?>
                    <div class="pull-left muted"><?php echo Yii::t('menu', 'Children Items') ?></div>
                <?php } ?>

                <?php
                foreach ($item->children as $childitem):
                    ?>
                    <div class="formRow pull-right">

                        <input type="text" name="MenuItem[<?php echo $childitem->id ?>][name]" 
                               value="<?php echo $childitem->name ?>" placeholder="<?php echo Yii::t('menuitem', 'Name') ?>"/>
                        <input type="text" name="MenuItem[<?php echo $childitem->id ?>][description]"
                               value="<?php echo $childitem->description ?>" placeholder="<?php echo Yii::t('menuitem', 'Description') ?>"/>
                        <select id="select_type" class="" name="MenuItem[<?php echo $childitem->id ?>][type]">
                            <?php foreach (MenuItem::getArrayType() as $type => $type_name): ?>
                                <option onclick="return select_type()" value="<?php echo $type ?>" 
                                        <?php echo $childitem->type == $type ? 'selected=""' : '' ?>>
                                            <?php echo $type_name ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                        <input type="text" name="MenuItem[<?php echo $childitem->id ?>][value]"
                               value="<?php echo $childitem->value ?>"  placeholder="<?php echo Yii::t('menuitem', 'Value') ?>"/>
                        <input type="text" name="MenuItem[<?php echo $childitem->id ?>][rank]"
                               value="<?php echo $childitem->rank ?>"  placeholder="<?php echo Yii::t('menuitem', 'Rank') ?>"/>
                        <a class="btn btn-mini btn-inverse" href="#" onclick="remove_item($(this), <?php echo $childitem->id ?>)">
                            <?php echo Yii::t('form', 'Delete') ?>
                        </a>

                        <div class="clear"></div>
                    </div>
                <?php endforeach; ?>
                <!--End Menu Item-->
            </div>

        <?php endforeach; ?>
    <?php endif; ?>
    <div class="clear"></div>
    <div id="createLink">
        <a class="btn btn-mini btn-primary" href="#" onclick="return add_item()">
            <?php echo Yii::t('menuitem', 'Create'); ?>
        </a>
        <a class="btn btn-mini btn-primary" href="#" onclick="return add_Childitem()">
            <?php echo Yii::t('menuitem', 'Create Children Item'); ?>
        </a>
    </div>
</div>
<?php $this->endWidget() ?>
<script>
            function remove_item(el, id)
            {
                el.parent().remove();
                hidden_input = '<input type="hidden" value="' + id + '" name="deleteItem[]"  />';
                $('#createLink').before(hidden_input);
            }
            function add_item()
            {
                id = jQuery.now();
                form = '<div class="formRow">';
                form += '<input type="text" name="MenuItem[' + id + '][name]" placeholder="<?php echo Yii::t('menuitem', 'Name') ?>" />';
                form += '<input type="text" name="MenuItem[' + id + '][description]" placeholder="<?php echo Yii::t('menuitem', 'Description') ?>" />';
                form += '<select name="MenuItem[' + id + '][type]">';
<?php foreach (MenuItem::getArrayType() as $type => $type_name): ?>
                    form += '<option value="<?php echo $type ?>" >';
                    form += '<?php echo $type_name ?>';
                    form += '</option>'
<?php endforeach; ?>
                form += '</select>';
                form += '<input type="text" name="MenuItem[' + id + '][value]" placeholder="<?php echo Yii::t('menuitem', 'Value') ?>"';
                form += 'value=""/>';
                form += '<input type="text" name="MenuItem[' + id + '][rank]" placeholder="<?php echo Yii::t('menuitem', 'Rank') ?>"';
                form += 'value=""/>';
                form += '<a class="btn btn-mini btn-inverse" href="#" onclick="$(this).parent().remove()">';
                form += '<?php echo Yii::t('form', 'Delete') ?>';
                form += '</a>';
                form += '<div class="clear"></div>';
                form += '</div>';

                $('#createLink').before(form);
                return false;
            }
            function add_Childitem()
            {
                id = jQuery.now();
                form = '<div class="pull-right">';
                form += '<input type="text" name="MenuItem[' + id + '][name]" placeholder="<?php echo Yii::t('menuitem', 'Name') ?>" />';
//                form += '<input type="text" name="MenuItem[' + id + '][description]" placeholder="<?php echo Yii::t('menuitem', 'Description') ?>" />';
                form += '<select name="MenuItem[' + id + '][type]">';
<?php foreach (MenuItem::getArrayType() as $type => $type_name): ?>
                    form += '<option value="<?php echo $type ?>" >';
                    form += '<?php echo $type_name ?>';
                    form += '</option>';
<?php endforeach; ?>
                form += '</select>';
                form += '<input type="text" name="MenuItem[' + id + '][value]" placeholder="<?php echo Yii::t('menuitem', 'Value') ?>"';
                form += 'value=""/>';
                form += '<input type="text" name="MenuItem[' + id + '][rank]" placeholder="<?php echo Yii::t('menuitem', 'Rank') ?>"';
                form += 'value=""/>';
                form += '</select>';

                form += '<select name="MenuItem[' + id + '][parent_id]" placeholder="<?php echo Yii::t('menuitem', 'Parent ID') ?>">';
                form += '<?php foreach ($this->model->items as $item): ?>';
                    form += '<option value="<?php echo $item->id ?>">';
                    form += '<?php echo $item->name ?>';
                    form += '</option>';
                    form += '<?php endforeach; ?>';
                form += '</select>';
                form += '<a class="btn btn-mini btn-inverse" href="#" onclick="$(this).parent().remove()">';
                form += '<?php echo Yii::t('form', 'Delete') ?>';
                form += '</a>';
                form += '<div class="clear"></div>';
                form += '</div>';

                $('#createLink').before(form);
                return false;
            }
</script>